package oo;

import java.util.Iterator;
import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;

public class ComboBoxItemConverter implements javax.faces.convert.Converter{

	@SuppressWarnings("unchecked")
	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {		
		List<SelectItem> selectItems = (List<SelectItem>)component.getAttributes().get("selectItems");
		Iterator<SelectItem> iterator = selectItems.iterator();
		while(iterator.hasNext()) {
			SelectItem selItem = iterator.next();
			ComboBoxItem comboBoxItem = (ComboBoxItem)selItem.getValue();
			if(comboBoxItem.getTranslatedvalue().equalsIgnoreCase(value)) {
				return comboBoxItem;
			}			
		}		
		return null;
	}

	public String getAsString(FacesContext context, UIComponent component, Object value) {
		if(value == null) {
			return "";
		}
		ComboBoxItem comboBoxItem = (ComboBoxItem) value;
		return comboBoxItem.getTranslatedvalue();		
	}
	

}
